package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.TableNameConstant;
import cc.rengu.igas.bomp.common.dao.BizTmpMapper;
import cc.rengu.igas.bomp.common.dao.MchntFeeInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.BizTmpMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.MchntFeeInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.BizTmp;
import cc.rengu.igas.bomp.common.entity.MchntFeeInfo;
import cc.rengu.igas.bomp.common.enums.QueryBizTypeEnum;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.common.util.InitParamUtil;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.MchntFeeInfoBean;
import cc.rengu.igas.bomp.facade.request.MchntFeeListQueryRequest;
import cc.rengu.igas.bomp.facade.response.MchntFeeListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.*;
import com.alibaba.fastjson.JSON;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户费率列表查询
 */
public class MchntFeeListQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntFeeListQueryRequest mchntFeeListQueryRequest = new MchntFeeListQueryRequest();
        ConvertUtil.convertOutput(mchntFeeListQueryRequest);
        return mchntFeeListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        MchntFeeListQueryRequest mchntFeeListQueryRequest = (MchntFeeListQueryRequest) request;
        MchntFeeListQueryResponse mchntFeeListQueryResponse = new MchntFeeListQueryResponse();
        BizResponse<MchntFeeListQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.IGAS);
        BeanUtil.beanCopy(mchntFeeListQueryRequest, mchntFeeListQueryResponse);

        String instId = mchntFeeListQueryRequest.getHeader().getInstId();
        String mchntNo = mchntFeeListQueryRequest.getMchntNo();
        String txnDate = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TXN_DATE);
        String bizType = mchntFeeListQueryRequest.getHeader().getBizType();
        if (StringUtil.isEmptyOrNull(bizType)) {
            rglog.error("bizType<{}>为NULL!,无法查询信息", bizType);
            bizResponse.setRespCode(RespCodeEnum.PARAM_ERROR.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.PARAM_ERROR.getRespDesc());
            bizResponse.setResult(mchntFeeListQueryResponse);
            return bizResponse;
        }

        if (QueryBizTypeEnum.QUERY_TEMPORARY_TABLE.getType().equals(bizType)) {
            /* 查询临时表数据 */
            BizTmpMapper bizTmpMapper = new BizTmpMapperImpl();
            //商户费率信息表
            List<BizTmp> feeBizTmpList = bizTmpMapper.selectBizTmpByMchntNo(instId, mchntNo, TableNameConstant.T_B_MCHNT_FEE_INFO);
            if (!CollectionUtils.isEmpty(feeBizTmpList)) {
                List<MchntFeeInfo> mchntFeeInfoList = new ArrayList<>();
                for (BizTmp bizTmp : feeBizTmpList) {
                    String feeInfoJson = InitParamUtil.getJsonString(bizTmp);
                    MchntFeeInfo mchntFeeInfo = JSON.parseObject(feeInfoJson, MchntFeeInfo.class);
                    mchntFeeInfoList.add(mchntFeeInfo);
                }
                /* 按照创建时间降序排序 */
                mchntFeeInfoList = mchntFeeInfoList.stream().sorted(Comparator.comparing(MchntFeeInfo::getCreateTime)).collect(Collectors.toList());
                Collections.reverse(mchntFeeInfoList);

                List<MchntFeeInfoBean> mchntFeeInfoBeans = new ArrayList<>();
                mchntFeeInfoList.forEach(item -> convFeeInfo(item, mchntFeeInfoBeans, txnDate));
                /** 查询结果赋值 */
                mchntFeeListQueryResponse.setMchntFeeInfoBeanList(mchntFeeInfoBeans);
            } else {
                rglog.error("商户号<{}>无商户费率信息!", mchntFeeListQueryRequest.getMchntNo());
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setResult(mchntFeeListQueryResponse);
                return bizResponse;
            }
        } else {
            // 商户费率信息表
            MchntFeeInfoMapper mchntFeeInfoMapper = new MchntFeeInfoMapperImpl();
            List<MchntFeeInfo> mchntFeeInfoList = mchntFeeInfoMapper.selectMchntFeeInfoByMchntNo(instId, mchntNo);
            if (null != mchntFeeInfoList && !mchntFeeInfoList.isEmpty()) {
                /* 按照创建时间降序排序 */
                mchntFeeInfoList = mchntFeeInfoList.stream().sorted(Comparator.comparing(MchntFeeInfo::getCreateTime)).collect(Collectors.toList());
                Collections.reverse(mchntFeeInfoList);
                List<MchntFeeInfoBean> mchntFeeInfoBeans = new ArrayList<>();
                mchntFeeInfoList.forEach(item -> convFeeInfo(item, mchntFeeInfoBeans, txnDate));
                /* 查询结果赋值 */
                mchntFeeListQueryResponse.setMchntFeeInfoBeanList(mchntFeeInfoBeans);
            } else {
                rglog.error("商户号<{}>无商户费率信息!", mchntFeeListQueryRequest.getMchntNo());
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setResult(mchntFeeListQueryResponse);
                return bizResponse;
            }
        }

        /* 按照接口转换基础产品信息并响应给请求方 */
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntFeeListQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntFeeListQueryResponse mchntFeeListQueryResponse = (MchntFeeListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntFeeListQueryResponse);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param mchntFeeInfo      数据库表结构对象
     * @param mchntFeeInfoBeans 接口对象
     */
    private void convFeeInfo(MchntFeeInfo mchntFeeInfo, List<MchntFeeInfoBean> mchntFeeInfoBeans, String txnDate) {
        if (DateUtil.compareDate(txnDate, mchntFeeInfo.getFeeEffectiveDate()) < 0
                || DateUtil.compareDate(mchntFeeInfo.getFeeExpiryDate(), txnDate) < 0) {
            rglog.info("删除费率id<{}>,prodCode<{}>,baseProdCode<{}>,feeType<{}>,feeInputMode<{}>.", mchntFeeInfo.getId(),
                    mchntFeeInfo.getProdCode(), mchntFeeInfo.getBaseProdCode(), mchntFeeInfo.getFeeType(), mchntFeeInfo.getFeeInputMode());
            return;
        }
        MchntFeeInfoBean mchntFeeInfoBean = new MchntFeeInfoBean();
        BeanUtil.beanCopy(mchntFeeInfo, mchntFeeInfoBean);
        mchntFeeInfoBeans.add(mchntFeeInfoBean);
    }
}
